﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Taxonomy;

namespace sptm2013
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void MenuFileExit_Click(object sender, RoutedEventArgs e)
        {
            
        }
       
        private void MenuAbout_Click(object sender, RoutedEventArgs e)
        {
                
        }

        private void MenuSaveConnectionProperties_Click(object sender, RoutedEventArgs e)
        {
                
        }

        private void ConnectButton_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(ConnectUriStringTextBox.Text))
            {
                var siteUrl = ConnectUriStringTextBox.Text;
                var getCollection = SampleConnectAndGetTaxonomy(siteUrl);
                TextBlockLog.Text = getCollection;
            }
        }

        private static string SampleConnectAndGetTaxonomy(string siteUrl)
        {
            StringBuilder sb = new StringBuilder();
            ClientContext clientContext = new ClientContext(siteUrl);
            TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
            TermStore termStore = taxonomySession.GetDefaultKeywordsTermStore();
            clientContext.Load(termStore,
                    store => store.Name,
                    store => store.Groups.Include(
                        group => group.Name,
                        group => group.TermSets.Include(
                            termSet => termSet.Name,
                            termSet => termSet.Terms.Include(
                                term => term.Name)
                        )
                    )
            );
            clientContext.ExecuteQuery();
            if (taxonomySession != null)
            {
                if (termStore != null)
                {
                    foreach (TermGroup group in termStore.Groups)
                    {
                        //Console.WriteLine("Group " + group.Name);
                        sb.AppendLine("Group " + group.Name);
                        foreach (TermSet termSet in group.TermSets)
                        {
                            //Console.WriteLine("TermSet " + termSet.Name);
                            sb.AppendLine("TermSet " + termSet.Name);
                            foreach (Term term in termSet.Terms)
                            {
                                //Writes root-level terms only.
                                //Console.WriteLine("Term " + term.Name);
                                sb.AppendLine("Term " + term.Name);
                            }
                        }
                    }
                }
            }
            return sb.ToString();
        }

        private void LoadFromCSV_Click(object sender, RoutedEventArgs e)
        {
                
        }
    }
}
